package com.pagepub.core.utils;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.pagepub.core.exception.ExceptionMessageParam;
import com.pagepub.core.exception.SysException;


public class CalendarUtil
{
	private static Log log = LogFactory.getLog(CalendarUtil.class);

	public static final String YMDOFYEAR = "year";

	public static final String YMDOFMONTH = "month";

	public static final String YMDOFDAY = "day";

	public static final String DIFFACTION_ADDITION = "addition";

	public static final String DIFFACTION_DECREASE = "decrease";

	public CalendarUtil()
	{
	}
	
	
	public static Timestamp getOracleDate(){
		Date date = new Date();
		Timestamp d=new Timestamp(date.getTime());
       return d;
   }
	
    /**
     * 将指定格式的时间String转为Date类型
     * @param dateStr String 待转换的时间String
     * @param pattern String 转换的类型
     * @throws ParseException
     * @return Date
     */
    public static Date convertStringToDate ( String dateStr , String pattern )
            throws ParseException
    {
        if ( dateStr == null )
        {
            return null ;
        }
        SimpleDateFormat sdf = new SimpleDateFormat ( pattern ) ;
        return sdf.parse ( dateStr ) ;
    }
	
	public static Timestamp convertOracleDate(Date date){
//		 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//	        try {
//	            String temp = sdf.format(date);
//	            date = sdf.parse(temp);            
//	        } catch (ParseException e) { 
//	            //e.printStackTrace();
//	            System.out.println("error: " + e);
//	     }
		
		Timestamp d=new Timestamp(date.getTime());
       return d;
   }
	
	public static Date convertDate(Date date){
       
        return date;
    }
	
	public static String getSysDate(Date date){
		SimpleDateFormat f=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        return f.format(date);
    }
	
	 

	
	public static Date getDate(){
		Date resultdate=new Date();
		Date date = new Date();
		
//		TimeZone.setDefault(TimeZone.getTimeZone("ETC/GMT-8"));
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.CHINA);
		//SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String datestr=sdf.format(date);
		try {
			resultdate=sdf.parse(datestr);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return resultdate;
	}

	/**
	 * 功能：返回d1-d2的天数

	 * 
	 * @param d1
	 *            Date
	 * @param d2
	 *            Date
	 * @return int
	 */
	public static int diff(Date d1, Date d2)
	{
		Calendar cal = Calendar.getInstance();
		cal.setTime(d1);
		int begind = cal.get(Calendar.DAY_OF_YEAR);
		cal.setTime(d2);
		int endd = cal.get(Calendar.DAY_OF_YEAR);
		return begind - endd;
	}

	/**
	 * 功能：返回d1-d2的天数

	 * 
	 * @param d1
	 *            Date
	 * @param d2
	 *            Date
	 * @return int
	 */
	public static int diff(java.sql.Date d1, java.sql.Date d2)
	{
		Calendar cal = Calendar.getInstance();
		cal.setTime(d1);
		int begind = cal.get(Calendar.DAY_OF_YEAR);
		cal.setTime(d2);
		int endd = cal.get(Calendar.DAY_OF_YEAR);
		return begind - endd;
	}

	/**
	 * 功能：dSrc日期加上num个YMD(YMD="year"或"month"或"day")后的日期
	 * 
	 * @param dSrc
	 *            Date
	 * @param num
	 *            int
	 * @param YMD
	 *            String
	 * @return Date
	 */
	public static Date differenceDate(Date dSrc, int num, String YMD)
	{
		Calendar cal = Calendar.getInstance();
		cal.setTime(dSrc);
		if (YMD.toLowerCase().equals("year"))
		{
			cal.set(Calendar.YEAR, cal.get(Calendar.YEAR) + num);
		}
		else if (YMD.toLowerCase().equals("month"))
		{
			cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) + num);
		}
		else if (YMD.toLowerCase().equals("day"))
		{
			cal.set(Calendar.DATE, cal.get(Calendar.DATE) + num);
		}
		Date retu = cal.getTime();
		return retu;
	}

	/**
	 * 功能：dSrc日期加上num个YMD(YMD="year"或"month"或"day")后的日期
	 * 
	 * @param dSrc
	 *            Date
	 * @param num
	 *            int
	 * @param YMD
	 *            String
	 * @return Date
	 */
	public static java.sql.Date differenceDate(java.sql.Date dSrc, int num, String YMD)
	{
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date(dSrc.getTime()));
		if (YMD.toLowerCase().equals("year"))
		{
			cal.set(Calendar.YEAR, cal.get(Calendar.YEAR) + num);
		}
		else if (YMD.toLowerCase().equals("month"))
		{
			cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) + num);
		}
		else if (YMD.toLowerCase().equals("day"))
		{
			cal.set(Calendar.DATE, cal.get(Calendar.DATE) + num);
		}
		java.sql.Date retu = new java.sql.Date(cal.getTime().getTime());
		return retu;
	}

	/**
	 * 功能：utilDate转成SqlDate
	 * 
	 * @param d
	 *            Date
	 * @return Date
	 */

	public static java.sql.Date utilDate2SqlDate(Date d)
	{
		return new java.sql.Date(d.getTime());
	}

	/**
	 * 功能：转换成格式化日期字串也可以转成带有日期时间也可以只有时间具体以formatStr变量来体现

	 * 
	 * @param d
	 *            Date
	 * @param formatStr
	 *            String 可以带日期也可以带时间只要该参数格式对都可以
	 * @return String
	 */
	public static String formateDate(Date d, String formatStr)
	{
		if (d == null)
			return "";
		if (formatStr.trim().equals(""))
			formatStr = "yyyy/MM/dd";
		SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
		return sdf.format(d);
	}

	/**
	 * 功能：转换成格式化日期字串(默认输出格式为yyyy/MM/dd)
	 * 
	 * @param d
	 *            Date
	 * @return String
	 */
	public static String formateDate(Date d)
	{
		if (d == null)
			return "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
		return sdf.format(d);
	}

	/**
	 * 功能：转换成格式化日期时间字串(默认输出格式为yyyy/MM/dd HH:mm:ss)
	 * 
	 * @param d
	 *            Date
	 * @return String
	 */
	public static String formateDateTime(Date d)
	{
		if (d == null)
			return "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		return sdf.format(d);
	}

	/**
	 * 功能：转换成格式化日期时间字串(默认输出格式为yyyy/MM/dd HH:mm:ss)
	 * 
	 * @param d
	 *            Date
	 * @return String
	 */
	public static String formateDateTime(java.sql.Date d)
	{
		if (d == null)
			return "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		return sdf.format(d);
	}

	/**
	 * 功能：转换成格式化日期字串也可以转成带有日期时间也可以只有时间具体以formatStr变量来体现

	 * 
	 * @param sqld
	 *            Date
	 * @param formatStr
	 *            String 指定自已的日期格式

	 * @return String
	 */
	public static String formateDate(java.sql.Date sqld, String formatStr)
	{
		if (sqld == null)
			return "";
		if (formatStr.trim().equals(""))
			formatStr = "yyyy/MM/dd";
		Date d = new Date(sqld.getTime());
		SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
		return sdf.format(d);
	}

	/**
	 * 功能：转换成格式化日期字串(默认输出格式为yyyy/MM/dd)
	 * 
	 * @param sqld
	 *            Date
	 * @return String
	 */
	public static String formateDate(java.sql.Date sqld)
	{
		if (sqld == null)
			return "";
		Date d = new Date(sqld.getTime());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
		return sdf.format(d);
	}

	/**
	 * 功能：取得当前日期字串(默认输出格式为yyyy/MM/dd)
	 * 
	 * @return String
	 */
	public static String getCurrentDate()
	{
		Date d = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
		return sdf.format(d);
	}

	/**
	 * 功能：取得当前日期字串并用formatStr来做格式化

	 * 
	 * @param formatStr
	 *            String
	 * @return String
	 */
	public static String getCurrentDate(String formatStr)
	{
		Date d = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
		return sdf.format(d);
	}

	/**
	 * 功能：取得当前日期时间字串(默认输出格式为yyyy/MM/dd HH:mm:ss)
	 * 
	 * @return String
	 */
	public static String getCurrentDateTime()
	{
		Calendar cal = Calendar.getInstance();
		Date d = new Date(cal.getTimeInMillis());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		return sdf.format(d);
	}

	/**
	 * 功能：取得当前日期时间字串

	 * 
	 * @param formatStr
	 *            String
	 * @return String
	 */
	public static String getCurrentDateTime(String formatStr)
	{
		Calendar cal = Calendar.getInstance();
		Date d = new Date(cal.getTimeInMillis());
		SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
		return sdf.format(d);
	}

	/**
	 * 取得当前日期时间，格式化，返回一个格式化后的日期时间
	 * 
	 * @param date
	 * @param formatStr
	 * @return
	 */
	public static Date parseCurrentDateTime(String formatStr)
	{
		Date now = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
		String nowStr = sdf.format(now);
		try
		{
			return sdf.parse(nowStr);
		}
		catch (ParseException e)
		{
			log.error("CalendarUtil.parseCurrentDateTime("+formatStr+")解析出错!");
			ExceptionMessageParam emp=new ExceptionMessageParam();
			emp.setKeyOrMessageText("CalendarUtil.parseCurrentDateTime("+formatStr+")解析出错!");
			emp.setMessage(true);
			throw new SysException(emp,e);
		}
	}

	public static Date formatDateByFormatStr(Date date1, String formatStr)
	{
		SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
		String date2Str = sdf.format(date1);
		try
		{
			return sdf.parse(date2Str);
		}
		catch (ParseException e)
		{
			log.error("CalendarUtil.formatDateByFormatStr("+date1+","+formatStr+")解析出错!");
			ExceptionMessageParam emp=new ExceptionMessageParam();
			emp.setKeyOrMessageText("CalendarUtil.parseCurrentDateTime("+date1+","+formatStr+")解析出错!");
			emp.setMessage(true);
			throw new SysException(emp,e);
		}
	}

	/**
	 * 功能：将dateStr字串转换成Util.Date
	 * 
	 * @param dateStr
	 *            String 日期格式的字串

	 * @param formatStr
	 *            String 对应于dateStr字串中的格式
	 * @return Date 成功返回Date否则返回null 例子1：String d1="2004/01/01";String
	 *         formatStr="yyyy/MM/dd";//注意formatStr必须对应于d1变量中日期格式

	 *         print(ExtCal.str2Date(d1,formatStr)) 例子2：String
	 *         d1="2004/1/1";String
	 *         formatStr="yyyy/M/d";//注意formatStr必须对应于d1变量中日期格式

	 *         print(ExtCal.str2Date(d1,formatStr))
	 */
	public static Date string2Date(String dateStr, String formatStr)
	{
		SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
		try
		{
			return sdf.parse(dateStr);
		}
		catch (ParseException e)
		{
			log.error("CalendarUtil.string2Date("+dateStr+","+formatStr+")解析出错!");
			ExceptionMessageParam emp=new ExceptionMessageParam();
			emp.setKeyOrMessageText("CalendarUtil.string2Date("+dateStr+","+formatStr+")解析出错!");
			emp.setMessage(true);
			throw new SysException(emp,e);
		}
	}

	// 时间转为字符串

	public static String date2String(Date date, String pattern)
	{
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);

		return sdf.format(date);
	}

	public static void main(String[] args)
	{
		// Calendar cal=Calendar.getInstance();

		// java.sql.Date d1=new java.sql.Date(ExtCal.string2Date("2004/11/25
		// 12:01:01","yyyy/MM/dd HH:mm:ss").getTime());
//		Date d2 = CalendarUtil.string2Date("2009-04-01 23:12:23", "yyyy-MM-dd HH:mm:ss");
//		Date d1=new Date();
//		System.out.println(convertDate(new Date()));
//		int a=CalendarUtil.diff(d1, d2);
//System.out.println(a);
//
//Timestamp stamp=convertOracleDate(new Date());
//System.out.println(stamp);
		// SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		// d=ExtCal.differenceDate(d,2,"month");
		// d=ExtCal.differenceDate(d,1,"month");
		// System.out.println("date==="+ExtCal.diffDate(ExtCal.utilDate2SqlDate(d1),ExtCal.utilDate2SqlDate(d2)));
//		System.out.println("date===" + CalendarUtil.formateDate(d2));
		
		
		SimpleDateFormat f=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
	       java.util.Date utilDate=new Date();   
	       
	       System.out.println(f.format(utilDate));
	       java.sql.Date sqlDate=new java.sql.Date(utilDate.getTime()); 
	       System.out.println(sqlDate);
	       java.sql.Time sTime=new java.sql.Time(utilDate.getTime());  
	       System.out.println(sTime);
	       java.sql.Timestamp stp=new java.sql.Timestamp(utilDate.getTime());
	      System.out.println(stp.toLocaleString());


	}
}

